<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('客户名称')" prop="customerName">
            <el-input v-model="form.customerName" :placeholder="$t('客户名称')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目收款')" prop="projectCollectionName">
            <el-input v-model="form.projectCollectionName" readonly :placeholder="$t('项目收款')" show-word-limit :maxlength="100">
              <template slot="append" icon="el-icon-search">
                <el-button
                  v-hasPermi="['anxin:invoice:edit']"
                  type="primary"
                  icon=""
                  size="mini"
                  @click="collectionHandleSelect"
                >{{ $t('查询项目收款') }}</el-button>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收款金额(元)')" prop="collectedAmount">
            <Money v-model="form.collectedAmount" :placeholder="$t('收款金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('纳税人识别号')" prop="taxpayerNumber">
            <el-input v-model="form.taxpayerNumber" :placeholder="$t('纳税人识别号')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开票公司')" prop="invoiceCompany">
            <el-select v-model="form.invoiceCompany" filterable :placeholder="$t('发票类型')">
              <el-option
                v-for="dict in invoiceCompanyOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('发票类型')" prop="invoiceType">
            <el-select v-model="form.invoiceType" filterable :placeholder="$t('发票类型')">
              <el-option
                v-for="dict in invoiceTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('税率(%)')" prop="taxRate">
            <Ratio v-model="form.taxRate" :placeholder="$t('税率')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开户行及账号')" prop="bankAccount">
            <el-input v-model="form.bankAccount" :placeholder="$t('开户行及账号')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('地址及电话')" prop="addressPhone">
            <el-input v-model="form.addressPhone" :placeholder="$t('地址及电话')" show-word-limit maxlength="200" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开票内容')" prop="invoiceContent">
            <el-input v-model="form.invoiceContent" :placeholder="$t('开票内容')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('邮寄地址')" prop="mailingAddress">
            <el-input v-model="form.mailingAddress" :placeholder="$t('邮寄地址')" show-word-limit maxlength="300" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开票人')" prop="drawer">
            <UserSelect
              v-model="form.drawer"
              :placeholder="this.$t('选择用户')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开票时间')" prop="invoiceTime">
            <DatePicker v-model="form.invoiceTime" :placeholder="$t('开票时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="anxin_invoice" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="anxin:invoice" :menu-key="menuKey" />
    </div>
    <!-- 添加或修改加班申请对话框 -->
    <el-dialog
      :title="$t('项目收款')"
      :visible.sync="formOpen"
      custom-class="fixedDialog"
      width="70%"
      :close-on-click-modal="false"
      :lock-scroll="false"
      append-to-body
    >
      <dealCollectionList ref="dealCollectionList" />
    </el-dialog>
  </div>
</template>

<script>
import {
  saveAnxinInvoiceApply,
  getAnxinInvoiceApply
} from '@/api/anxin/invoice/anxinInvoiceApply'
import dealCollectionList from './dealCollectionList.vue'
export default {
  components: {
    dealCollectionList
  },
  data() {
    return {
      // 菜单标识
      menuKey: 'anxin_invoice',
      // 开票类型
      invoiceTypeOptions: [],
      // 开票公司
      invoiceCompanyOptions: [],
      formOpen: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        actTitle: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        customerName: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        projectCollection: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        collectedAmount: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        projectCollectionName: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        taxpayerNumber: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        invoiceType: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        taxRate: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        invoiceCompany: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        drawer: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        invoiceTime: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        applyTime: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        applyAmount: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      },
      // 遮罩层
      loading: false,
      // 是否隐藏其它按钮
      otherBtnHide: false,
      // 操作按钮默认不展示
      buttonShow: true
    }
  },
  created() {
    this.getDicts('invoice_type').then((response) => {
      this.invoiceTypeOptions = response.data
    })
    this.getDicts('invoice_company').then((response) => {
      this.invoiceCompanyOptions = response.data
    })
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      console.log(data)
      this.loading = true
      if (data != null && data.id !== undefined) {
        getAnxinInvoiceApply(data.id).then((response) => {
          this.form = response.data
          if (data.isCopy) {
            this.form.id = undefined
            this.form.createBy = undefined
            this.form.createTime = undefined
            this.form.updateBy = undefined
            this.form.updateTime = undefined
          }
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        customerName: undefined,
        projectCollection: undefined,
        addressPhone: undefined,
        taxpayerNumber: undefined,
        bankAccount: undefined,
        invoiceType: undefined,
        taxRate: undefined,
        invoiceContent: undefined,
        invoiceCompany: undefined,
        mailingAddress: undefined,
        drawer: this.$store.state.user.userId,
        collectedAmount: undefined,
        invoiceTime: new Date(),
        actId: undefined,
        actStatus: undefined,
        actTitle: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        fileList: [],
        projectCollectionName: undefined,
        collectionMoney: undefined,
        isCopy: false
      }
      this.resetForm('form')
    },
    /** 项目收款数据回显处理 */
    projectCollectionHandle(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        this.form.projectCollectionName =
          data.dealName + '(' + this.moneyFormat(data.collectionMoney) + ')'
        this.form.projectCollection = data.id
        this.form.collectedAmount = data.collectionMoney
        // this.form.collectionMoney = this.moneyFormat(data.collectionMoney)
        this.loading = false
      } else {
        this.loading = false
      }
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    /** 新增按钮操作 */
    collectionHandleSelect() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealCollectionList.init(null)
      })
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveAnxinInvoiceApply(this.form).then((response) => {
              if (response.code === 200) {
                // 点击的按钮是保存并启动流程
                this.form.id = response.data.id
                if (type === 'process') {
                  // 启动流程
                  this.startProcess(response.data.id)
                  // 点击的仅保存
                } else {
                  this.msgSuccess(this.$t('保存成功'))
                  // 关闭form页面
                  this.closeForm()
                }
                resolve()
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject()
              }
            })
          } else {
            this.locationError(document)
          }
        })
      })
    },
    // 启动流程
    startProcess: function(dataId) {
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = '申请开票:' + this.form.customerName + '(' + this.moneyFormat(this.form.collectedAmount) + ')'
      // 业务表名
      processData.dataTable = 'anxin_invoice_apply'
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/anxin/invoice/anxinInvoiceApplyForm'
      processData.viewPath = '@/views/anxin/invoice/anxinInvoiceApplyView'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = this.menuKey
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    }
  }
}
</script>
